package band.wukong.practice.java.core.topic.concurrency.thread.cooperation;

/**
 * @author wukong(wukonggg@139.com)
 */
public class WaitAndNotifySample {

    final static Object lock = new Object();

    public static void main(String[] args) {
        Thread t1 = new T1();
        Thread t2 = new T2();

        t1.start();
        t2.start();
    }

    public static class T1 extends Thread {
        @Override
        public void run() {
            synchronized (lock) {
                System.out.println(System.currentTimeMillis() + ": T1 start!");
                System.out.println(System.currentTimeMillis() + ": T1 wait for lock!");
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(System.currentTimeMillis() + ": T1 end!");
            }
        }
    }

    public static class T2 extends Thread {
        @Override
        public void run() {
            synchronized (lock) {
                System.out.println(System.currentTimeMillis() + ": T2 start! Notify one thread.");
                lock.notify();
                System.out.println(System.currentTimeMillis() + ": T2 end!");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }
    }
}
